package com.ustc.mvc.util;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

public class XMLUtil {
	public XMLUtil(){}
	
	public static Map<String, XmlBean> jiexi(String xmlpath) throws Exception{
		Map<String, XmlBean> map = new HashMap<String, XmlBean>();
		SAXBuilder builder = new SAXBuilder();
		Document doc = builder.build(new File(xmlpath));
		Element root = doc.getRootElement();
		Element actionmap = root.getChild("action-mapping");
		List<Element> action = actionmap.getChildren();
		for(Element child: action){
			XmlBean bean = new XmlBean();
			String path = child.getAttributeValue("path");
			bean.setPath(path);
			String  name = child.getAttributeValue("name");
			bean.setBeanname(name);
			String actionclass = child.getAttributeValue("type");
			bean.setActionclass(actionclass);
			Element formbean = root.getChild("formbeans");
			List<Element> formbeans = formbean.getChildren();
			for(Element f:formbeans){
				if(name.equals(f.getAttributeValue("name"))){
					bean.setBeanclass(f.getAttributeValue("class"));
				}
			}
			
			List<Element> forwordlist = child.getChildren();
			Map<String, String> forward = new HashMap<String, String>();
			for(Element e: forwordlist){
				String fname = e.getAttributeValue("name");
				String fvalue = e.getAttributeValue("value");
				forward.put(fname, fvalue);
			}
			bean.setForwardmap(forward);
			map.put(path, bean);
		}
		return map;
	}
}
